<template>
  <div>
    <van-nav-bar
      title="修改昵称"
      left-text="返回"
      right-text="保存"
      left-arrow
      @click-left="$emit('clickLeft')"
      @click-right="onClickRight"
    />
    <!-- 输入框 -->
    <van-field
      v-model="message"
      type="textarea"
      maxlength="7"
      placeholder="请输入新昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { patchuSerProfile } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      message: ''
    }
  },
  props: {
    value: {
      type: String,
      require: true
    }
  },
  created () {
    this.message = this.value
  },

  methods: {
    async onClickRight () {
      try {
        const res = await patchuSerProfile({
          name: this.message
        })
        this.$emit('on_right')
        this.$emit('input', this.message)
        Toast.success('更新成功')

        console.log(res)
      } catch (err) {
        console.dir(err)
        if (err.response && err.response.status === 409) {
          return Toast.fail('名字重复了')
        }
        Toast.fail('更新失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
